package com.tskj.leonsu.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tskj.leonsu.aop.annotation.SystemControllerLog;
import com.tskj.leonsu.service.BathRoomService;
import com.tskj.leonsu.service.LoginService;
import com.tskj.leonsu.service.StudentService;
import com.tskj.leonsu.utility.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * 洗浴服务
 * @author LeonSu
 */
@Controller
@RequestMapping("/bathroom")
public class BathRoomController {

    @Autowired
    private BathRoomService bathRoomService;

    @Autowired
    private LoginService loginService;

    @Autowired
    private StudentService studentService;

    /**
     * 获取浴室列表
     * @return
     */
    @GetMapping("/list")
    @ResponseBody
    public JSONObject getList(@RequestParam String token) {
        String adUserName = loginService.getAdUserName(token);
        String sex = studentService.getSex(adUserName);
        JSONObject ret = new JSONObject();
        ret.put("code", 0);
        ret.put("data", bathRoomService.getBathRoomList(sex));
        return ret;
    }

    /**
     * 获取浴室时间表
     * @param bathId
     * @param date
     * @return
     */
    @GetMapping("/timeList")
    @ResponseBody
    public JSONObject getTimeList(@RequestParam String bathId, @RequestParam String date) {
        JSONObject ret = new JSONObject();
        ret.put("code", 0);
        ret.put("data", bathRoomService.getTimeAndCountList(bathId, date));
        return ret;
    }

    /**
     * 保存浴室订单
     * @param data
     * @return
     */
    @PostMapping("/save")
    @ResponseBody
    public JSONObject saveOrder(@RequestParam String token, @RequestBody JSONObject data) {
        String adUserName = loginService.getAdUserName(token);
        JSONObject student = studentService.getStudentInfo(adUserName);
        return bathRoomService.saveOrder(student, data);
    }


    @GetMapping("/checkStopTime")
    @ResponseBody
    public JSONObject checkStopTime(@RequestParam String token, @RequestParam String date) {
        String adUserName = loginService.getAdUserName(token);
        if (StringUtils.isEmpty(date)) {
            return bathRoomService.checkStopTime(adUserName, Tools.today());
        } else {
            return bathRoomService.checkStopTime(adUserName, date);
        }
    }
}
